﻿using BCL.WPFComponent.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Devices;
using DevicesWPF.Logic;
using System.Collections.ObjectModel;

namespace DevicesWPF
{
    /// <summary>
    /// UCDevicesCollectionConfig.xaml 的交互逻辑
    /// </summary>
    public partial class UCTestItemConfig : BaseFunctionControl
    {

        private List<string> codes;
        public UCTestItemConfig()
        {
            InitializeComponent();
            if (DevicesCollection.Devices != null && DevicesCollection.Devices.Any(o => o.Info.Brand == "爱科来(ARKRAY)"))
            {
                List<DevicesInformation> devs = DevicesCollection.Devices?.Where(o => o.Info.Brand == "爱科来(ARKRAY)").Select(o => o.Info).ToList();
                List<DevAutoConfigInfo> infos = new List<DevAutoConfigInfo>();
                foreach (var item in devs)
                {
                    DevAutoConfigInfo info = DevicesLogic.GetDevicesTest(item.Code);
                    if (info == null)
                        info = new DevAutoConfigInfo { Code=item.Code, Selected=false  };
                    info.Name = item.DisplayName;
                    infos.Add(info);
                }
                gridControl1.ItemsSource = infos;
                gridControl1.SelectedIndex = 0;
            }

        }

        private void TextBlock_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //throw new NotImplementedException();
        }

        public override void LanguageBinding()
        {

        }
        public override void ReloadData(object data)
        {
            base.ReloadData(data);
        }

        private void gridControl1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DevAutoConfigInfo info = gridControl1.SelectedItem as DevAutoConfigInfo;
            if (info == null)
                return;

            gridControl2.ItemsSource = info.Tests;
            if (info.Tests != null && info.Tests.Count>0)
                gridControl2.SelectedIndex = 0;
        }

        private void gridControl2_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DevicesTest info = gridControl2.SelectedItem as DevicesTest;
            if (info == null)
                return;
            DevAutoConfigInfo dinfo = gridControl1.SelectedItem as DevAutoConfigInfo;
            if (info == null)
                return;
            IDevices devices = DevicesCollection.Devices.FirstOrDefault(o=>o.Info.Code== dinfo.Code);

            codes= devices.Config.ResultConfig[0].Items.Select(o => o.Code).Distinct().ToList();
            ComboBoxitems.ItemsSource = codes;
            

            gridControl3.ItemsSource = null;
            gridControl3.ItemsSource = info.Items;
        }

        /// <summary>
        /// 添加检查项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DevAutoConfigInfo info = gridControl1.SelectedItem as DevAutoConfigInfo;
            if (info == null)
                return;
            //gridControl2.ItemsSource = null;
            //List<DevicesTest> tests = DevicesLogic.GetDevicesTest(info.Code);
            ObservableCollection<DevicesTest> tests = gridControl2.ItemsSource as ObservableCollection<DevicesTest>;
            //gridControl2.ItemsSource = null;
            if (tests == null)
                tests = new ObservableCollection<DevicesTest>();

            tests.Add(new DevicesTest {Name = "新建检查" });

            if (gridControl2.ItemsSource == null)
                gridControl2.ItemsSource = tests;
        }

        /// <summary>
        /// 新建检查项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageAddTestItem_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DevicesTest tests = gridControl2.SelectedItem as DevicesTest;
            if (tests.Items == null)
            {
                tests.Items = new ObservableCollection<DevicesTestItem>();
                gridControl3.ItemsSource = tests.Items;
            }

            tests.Items.Add(new DevicesTestItem { Code= codes[0] });

            //ObservableCollection<DevicesTestItem> items = gridControl3.ItemsSource as ObservableCollection<DevicesTestItem>;
            //if (items == null)
            //    items=new ObservableCollection<DevicesTestItem>();
            //items.Add(new DevicesTestItem { Name="新指标"  });
            //if (gridControl3.ItemsSource == null)
            //    gridControl3.ItemsSource = items;
        }

        private void ImageDeleteImte_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DevicesTest test = gridControl2.SelectedItem as DevicesTest;
            if (test == null)
                return;
            if (BCL.WPFComponent.UCMessageBox.Show("确定要删除此检查嘛？", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                ObservableCollection<DevicesTest> tests = gridControl2.ItemsSource as ObservableCollection<DevicesTest>;
                tests.Remove(test);
            }

        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DevAutoConfigInfo info = gridControl1.SelectedItem as DevAutoConfigInfo;
            if (info == null)
                return;
            DevicesLogic.Save(info.Code, info);
            BCL.WPFComponent.UCMessageBox.Show("保存成功");
        }

    }


}
